package com.xbts.web.controller.editor;

import java.util.List;

import com.xbts.common.utils.SecurityUtils;
import com.xbts.system.domain.SeiMaterial;
import com.xbts.system.domain.SeiTemplate;
import com.xbts.system.service.ISeiTemplateService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.service.ISeiMaterialService;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 素材Controller
 * @author xbts
 * @date 2023-08-24
 */
@Api("素材")
@RestController
@RequestMapping("/system/material")
public class SeiMaterialController extends BaseController {

    @Autowired
    private ISeiMaterialService seiMaterialService;

    @Autowired
    private ISeiTemplateService seiTemplateService;

    /**
     * 查询素材列表
     */
    @ApiOperation("查询素材列表")
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SeiMaterial seiMaterial) {
        List<SeiMaterial> list = seiMaterialService.selectMaterialList(seiMaterial);
        return success(list);
    }

    /**
     * 我的素材
     */
    @ApiOperation("我的素材列表")
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @PostMapping("/my/list")
    public TableDataInfo myList(@RequestBody SeiMaterial seiMaterial) {
        startPage(seiMaterial);
        seiMaterial.setMaterialAffiliation(SecurityUtils.getUserId());
        List<SeiMaterial> list = seiMaterialService.searchMaterialList(seiMaterial);
        return getDataTable(list);
    }

    /**
     * 查询素材
     *
     * @return 素材列表
     */
    @ApiOperation("查询素材列表")
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @PostMapping("/materialList")
    public TableDataInfo materialList() {
    return getDataTable(seiMaterialService.selectMaterial());
    }

    /**
     * 条件查询素材列表
     */
    @ApiOperation("条件查询素材列表")
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @PostMapping("/search")
    public TableDataInfo search(@RequestBody SeiMaterial seiMaterial) {
        startPage(seiMaterial);
        List<SeiMaterial> list = seiMaterialService.searchMaterialList(seiMaterial);
        return getDataTable(list);
    }

    /**
     * 获取素材详细信息
     */
    @ApiOperation("获取素材详细信息")
    @ApiImplicitParam(name = "materialId", value = "素材ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") String materialId) {
        return success(seiMaterialService.selectSeiMaterialById(materialId));
    }

    /**
     * 新增一级指标
     */
    @ApiOperation("新增一级指标")
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "素材", businessType = BusinessType.INSERT)
    @GetMapping("/addParent")
    public AjaxResult addParent(@Validated String name) {
        return toAjax(seiMaterialService.insertMaterialType(name));
    }

    /**
     * 添加子类
     */
    @ApiOperation("新增子类")
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "素材", businessType = BusinessType.INSERT)
    @PostMapping("/addSon")
    public AjaxResult addSon(@Validated @RequestBody SeiMaterial seiMaterial) {
        seiMaterial.setType(2);
        return toAjax(seiMaterialService.addSon(seiMaterial));
    }

    /**
     * 新增素材
     */
    @ApiOperation("新增素材")
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "素材", businessType = BusinessType.INSERT)
    @PostMapping("/addMaterial")
    public AjaxResult addMaterial(@Validated @RequestBody SeiMaterial seiMaterial) {
        seiMaterial.setType(3);
        return toAjax(seiMaterialService.addMaterial(seiMaterial));
    }
    /**
     * 修改素材或添加内容
     */
    @ApiOperation("修改素材或添加内容")
    @PreAuthorize("@ss.hasPermi('system:material:edit')")
    @Log(title = "素材", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody SeiMaterial seiMaterial) {
        return toAjax(seiMaterialService.updateMaterial(seiMaterial));
    }

    /**
     * 批量删除素材
     */
    @ApiOperation("批量删除素材")
    @ApiImplicitParam(name = "materialIds", value = "素材ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "素材", businessType = BusinessType.DELETE)
	@PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> ids) {
        return toAjax(seiMaterialService.batchDeleteMaterial(ids));
    }
    /**
     * 删除素材
     */
    @ApiOperation("删除素材")
    @ApiImplicitParam(name = "materialIds", value = "素材ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "素材", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(String materialId) {
        return toAjax(seiMaterialService.deleteMaterial(materialId));
    }
    /**
     * 查询素材类型
     */
    @ApiOperation("查询素材类型")
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @PostMapping("/type")
    public TableDataInfo typeList(@RequestBody SeiMaterial seiMaterial) {
        return getDataTable(seiMaterialService.selectMaterialType(seiMaterial));
    }
    /**
     * 查询一级
     */
    @ApiOperation("查询一级")
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping("/grandparent")
    public TableDataInfo grandParentList() {
        return getDataTable(seiMaterialService.selectGrandParent());
    }
    /**
     * 查询二级
     */
    @ApiOperation("查询二级")
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping("/parent")
    public TableDataInfo parentList(String parentId) {
        return getDataTable(seiMaterialService.selectParent(parentId));
    }


    /**
     * 生成word
     */
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @ApiOperation("生成word")
    @PostMapping("/word")
    public void generateWord(HttpServletRequest request, HttpServletResponse response, @RequestBody  SeiMaterial seiMaterial) {
        seiTemplateService.outPutWord(request, response, seiMaterial.getContent());
    }

    @ApiOperation("导入word")
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        return success(seiTemplateService.importData(file));
    }

}
